<?php
include_once('classes/class.mssql.php');
session_start();
$comtec_id_operation  = $_GET["comtec_id_operation"];
$comtec_id_machine    = $_GET["comtec_id_machine"];
$comtec_id_mainteiner = $_SESSION["comtec_id_mainteiner"];
$token                = $_GET["token"];

$db = new MSSQL();
$result = $db->fetch_array($db->query("SELECT cod_mainteiner,comtec_id_configman FROM machines WHERE comtec_id_machine = $comtec_id_machine"));   
$cod_mainteiner = $result['cod_mainteiner'];
$comtec_id_configman = $result['comtec_id_configman'];

$result = $db->fetch_array($db->query(
     "SELECT (r.name+' '+r.lastname1+' '+r.lastname2) as encargado,o.operation_type,
       
       DATEPART(day,m.date_next_maintenance) as next_day,                     
       DATEPART(month,m.date_next_maintenance) as next_month,
       DATEPART(yyyy,m.date_next_maintenance) as next_year, 
         
       DATEPART(day,o.operation_date_start) as op_day1,                     
       DATEPART(month,o.operation_date_start) as op_month1,
       DATEPART(yyyy,o.operation_date_start) as op_year1, 
       DATEPART(hour,o.operation_date_start) as hour1,
       DATEPART(minute,o.operation_date_start) as minute1, 
       
       DATEPART(day,o.operation_date_end) as op_day2,                     
       DATEPART(month,o.operation_date_end) as op_month2,
       DATEPART(yyyy,o.operation_date_end) as op_year2,  
       DATEPART(hour,o.operation_date_end) as hour2, 
       DATEPART(minute,o.operation_date_end) as minute2,  
        
       o.diagnosis,o.changed_parts,o.value_parts,o.value_work,o.value_others,o.value_total,o.notes,o.notes_for_owner,
       o.delay,r.comtec_id_responsible,o.revised
      FROM (operation as o INNER JOIN machines as m ON o.comtec_id_machine=m.comtec_id_machine) INNER JOIN 
            responsible as r ON o.comtec_id_responsible = r.comtec_id_responsible WHERE o.comtec_id_operation = $comtec_id_operation"
)); 

 $operation_type        = $result['operation_type'];
 $comtec_id_responsible = $result['comtec_id_responsible'];
 $encargado             = $result['encargado'];
 $diagnosis             = $result['diagnosis'];
 $changed_parts         = $result['changed_parts'];
 $notes                 = $result['notes'];
 
 $next_day              = $result['next_day'];
 $next_month            = $result['next_month'];
 $next_year             = $result['next_year'];
 
 $op_day1               = $result['op_day1'];
 $op_month1             = $result['op_month1'];
 $op_year1              = $result['op_year1'];
 $hour1                 = $result['hour1'];
 $minute1               = $result['minute1'];
 
 $op_day2               = $result['op_day2'];
 $op_month2             = $result['op_month2'];
 $op_year2              = $result['op_year2'];
 $hour2                 = $result['hour2'];
 $minute2               = $result['minute2'];
 
 $value_parts = $result['value_parts'];
 $value_work = $result['value_work'];
 $value_others = $result['value_others'];
 $value_total = $result['value_total'];
 
 $delay      = $result['delay'];
 $revised    = $result['revised'];
 $notes_for_owner = $result['notes_for_owner'];

function extra($x){
 if ($x <= 9){
  $x = "0".$x;   
 }   
 return $x;   
}

$texto = '<fieldset class="curved">
 <legend>Editar</legend>
  <table style="margin: 10px auto; border-spacing: 10px;">
      <tr><div>
          <td width="200"><label>M&aacute;quina</label></td>
          <td><input DISABLED size="40" type="text" value='.$cod_mainteiner.'></td>
          </div>
      </tr>';
  $texto .= '<tr><div>
             <td width="200"><label>Tipo de operaci&oacute;n</label></td>
             <td><select id="operation_type" onchange="javascript:operation_type()" style="width:257px;">';
              switch ($operation_type){
               case 0: 
                      $texto .= ' <option value="0" selected>Selecci&oacute;n...</option>
                                  <option value="1">Mantenci&oacute;n</option>
                                  <option value="2">Emergencia</option>
                                  <option value="3">Instalaci&oacute;n</option>
                                  <option value="4">Reparaci&oacute;n</option>
                                 ';
               break;
               case 1: 
                      $texto .= ' <option value="0">Selecci&oacute;n...</option>
                                  <option value="1" selected>Mantenci&oacute;n</option>
                                  <option value="2">Emergencia</option>
                                  <option value="3">Instalaci&oacute;n</option>
                                  <option value="4">Reparaci&oacute;n</option>
                                 ';
               break;
               case 2: 
                      $texto .= ' <option value="0">Selecci&oacute;n...</option>
                                  <option value="1">Mantenci&oacute;n</option>
                                  <option value="2" selected>Emergencia</option>
                                  <option value="3">Instalaci&oacute;n</option>
                                  <option value="4">Reparaci&oacute;n</option>
                                 ';
               break;
               case 3: 
                      $texto .= ' <option value="0">Selecci&oacute;n...</option>
                                  <option value="1">Mantenci&oacute;n</option>
                                  <option value="2">Emergencia</option>
                                  <option value="3" selected>Instalaci&oacute;n</option>
                                  <option value="4">Reparaci&oacute;n</option>
                                 ';
               break;
               case 4: 
                      $texto .=  '<option value="0">Selecci&oacute;n...</option>
                                  <option value="1">Mantenci&oacute;n</option>
                                  <option value="2">Emergencia</option>
                                  <option value="3">Instalaci&oacute;n</option>
                                  <option value="4" selected>Reparaci&oacute;n</option>
                                 ';
               break;
              } 
  $texto .=  '</select>
              </td>'; 
  $texto .=  '<td width="200"><label>Encargado</label></td>
              <td><select id="comtec_id_responsible" style="width:257px;">
                    <option value="0" selected>Selecci&oacute;n...</option>';
                    $responsible = $db->query("SELECT comtec_id_responsible, name, lastname1, lastname2, status_reg FROM responsible WHERE comtec_id_mainteiner = $comtec_id_mainteiner ");
                        while ($r = sqlsrv_fetch_array($responsible,SQLSRV_FETCH_ASSOC))
                        {
                            if ($r['status_reg'] == 1)
                            {
                             if ($comtec_id_responsible==$r['comtec_id_responsible']){
                             $texto .= "<option value=".$r['comtec_id_responsible']." selected>".$r['name']." ".$r['lastname1']." ".$r['lastname2']."</option>";
                             }else {
                              $texto .= "<option value=".$r['comtec_id_responsible'].">".$r['name']." ".$r['lastname1']." ".$r['lastname2']."</option>";    
                             }
                             
                            }
                        }
  $texto .=  '</select></td></div></tr>';
  $texto .=  '<tr><div>
               <td width="200"><label>Diagn&oacute;stico</label></td>
               <td colspan=4><textarea rows="4" cols="91" style="resize:none" id="diagnosis">'.$diagnosis.'</textarea></td>
              </div></tr>';
  $texto .=  '<tr><div>
                <td width="200"><label>Repuestos cambiados</label></td>
                <td colspan=4><textarea rows="4" cols="91" style="resize:none" id="changed_parts">'.$changed_parts.'</textarea></td>
              </div></tr>';
  $texto .=  '<tr><div>
                <td width="200"><label>Notas</label></td>
                <td colspan=4><textarea rows="3" cols="91" style="resize:none" id="notes">'.$notes.'</textarea></td>
             </div></tr>';
  $texto .=  '<tr id="next_date"'; 
  if($operation_type!=1){
   $texto .=  'style="display: none"';
  }
  $texto .=  '><td width="200">Fecha prox. Mantenci&oacute;n</td><td>';
               $optionsXML = simplexml_load_file("../xml/days.xml"); 
               $options =  $optionsXML->xpath("//day/text()");
               $texto .= '<select id="next_day">';
               while(list( $key , $value) = each($options)) 
               {                       
                if ($next_day == $value) { 
                 $texto .= '<option value='.$value.' selected >' . $value . '</option>';
                }else {
                       $texto .= '<option value='.$value.'>' . $value . '</option>';                  
                      }
               }
               $texto .='</select>';
               
               $optionsXML = simplexml_load_file("../xml/months.xml"); 
                  $options =  $optionsXML->xpath("//month/text()");
                  $texto .= '<select id="next_month">';
                  while(list( $key , $value) = each($options)) {  
                   if ($next_month == $key) { $texto .= '<option value='.$value.' selected >' . $value . '</option>';}   
                   else {$texto .= '<option value='.$value.'>' . $value . '</option>';}
                  }       
               $texto .='</select>';
               
               $optionsXML = simplexml_load_file("../xml/years.xml"); 
                  $options =  $optionsXML->xpath("//year/text()");
                  $texto .= '<select id="next_year">';
                  while(list($key,$value) = each($options)) {   
                   if ($next_year == $value) { $texto .= '<option value='.$value.' selected >' . $value . '</option>';}   
                   else {$texto .= '<option value='.$value.'>' . $value . '</option>';}
                  }
                 $texto .='</select>';
               
  $texto .=  '</td>
              </tr>';      
  if($_SESSION['users']=="administratives-2" || $_SESSION['users']=="managers"){
    $texto .= '<tr><div>
                <td width="200"><label>Valor mano de obra</label></td>
                <td><input type="text" size="40" id="value_work" value="'.$value_work.'" onchange="javascript:addValue()" /></td>
                <td width="200"><label>Valores repuestos</label></td>
                <td><input type="text" size="40" id="value_parts" value="'.$value_parts.'" onchange="javascript:addValue()" /></td>
               </tr>
               <tr>
                <td width="200"><label>Otros valores</label></td>
                <td><input type="text" size="40" id="value_others" value="'.$value_others.'" onchange="javascript:addValue()" /></td>
                <td width="200"><label>Valor total</label></td>
                <td><input type="text" size="40" DISABLED id="value_total" value="'.$value_total.'" /></td></div>
               </tr>';
  }
  $texto .= '<tr><td width="200"><label>Fecha de inicio</label></td><td>';  
   $optionsXML = simplexml_load_file("../xml/days.xml"); 
                     $options =  $optionsXML->xpath("//day/text()");
                     $texto .= '<select id="op_day1" onchange="javascript:setDelay()">';
                     while(list( $key , $value) = each($options)) {      
                      if ($op_day1 == $key) { $texto .= '<option value='.extra($key).' selected >' . $value . '</option>';}   
                      else {$texto .= '<option value='.extra($key).'>' . $value . '</option>';}
                     }
   $texto .=  '</select>';  
   
   $optionsXML = simplexml_load_file("../xml/months.xml"); 
                      $options =  $optionsXML->xpath("//month/text()");
                      $texto .= '<select id="op_month1" onchange="javascript:setDelay()">';
                      while(list( $key , $value) = each($options)) {  
                       if ($op_month1 == $key) { $texto .= '<option value='.extra($key).' selected >' . $value . '</option>';}   
                       else {$texto .= '<option value='.extra($key).'>' . $value . '</option>';}
                      }
   $texto .=  '</select>';
   $optionsXML = simplexml_load_file("../xml/years.xml"); 
                      $options =  $optionsXML->xpath("//year/text()");
                      $texto .= '<select id="op_year1" onchange="javascript:setDelay()">';
                      while(list($key,$value) = each($options)) {   
                       if ($op_year1 == $value) { $texto .= '<option value='.$value.' selected >' . $value . '</option>';}   
                       else {$texto .= '<option value='.$value.'>' . $value . '</option>';}
                      }
  $texto .=  '</select>';
  $texto .=  '<td width="200"><label>Hora de inicio</label></td><td>';
              $optionsXML = simplexml_load_file("../xml/hours.xml"); 
                  $options =  $optionsXML->xpath("//hour/text()");
                  $texto .= '<select id="hour1" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) {   
                   if($value==$hour1){
                    $texto .= '<option value='.$value.' selected>' . $value . '</option>';
                   }else{$texto .= '<option value='.$value.'>' . $value . '</option>';}
                  }
  $texto .=  '</select>';
                  $optionsXML = simplexml_load_file("../xml/minutes.xml"); 
                  $options =  $optionsXML->xpath("//minute/text()");
                  $texto .= '<select id="minute1" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) {  
                   if($minute1==$value){   
                    $texto .= '<option value='.$value.' selected>' . $value . '</option>';
                   }else{$texto .= '<option value='.$value.'>' . $value . '</option>';}
                  }
  $texto .=  '</select>';                
  $texto .=  '</td></tr>'; 
  
  $texto .=  '<tr>
              <td width="200"><label>Fecha de fin</label></td><td>';
              $optionsXML = simplexml_load_file("../xml/days.xml"); 
              $options =  $optionsXML->xpath("//day/text()");
              $texto .= '<select id="op_day2" onchange="javascript:setDelay()">';
              while(list( $key , $value) = each($options)) {      
               if ($op_day2 == $key) { $texto .= '<option value='.extra($key).' selected >' . $value . '</option>';}   
               else {$texto .= '<option value='.extra($key).'>' . $value . '</option>';}
              }
  $texto .=  '</select>';
               $optionsXML = simplexml_load_file("../xml/months.xml"); 
                      $options =  $optionsXML->xpath("//month/text()");
                      $texto .= '<select id="op_month2" onchange="javascript:setDelay()">';
                      while(list( $key , $value) = each($options)) {  
                       if ($op_month2 == $key) { $texto .= '<option value='.extra($key).' selected >' . $value . '</option>';}   
                       else {$texto .= '<option value='.extra($key).'>' . $value . '</option>';}
                      }
  $texto .=  '</select>';  
                      $optionsXML = simplexml_load_file("../xml/years.xml"); 
                      $options =  $optionsXML->xpath("//year/text()");
                      $texto .= '<select id="op_year2" onchange="javascript:setDelay()">';
                      while(list($key,$value) = each($options)) {   
                       if ($op_year2 == $value) {  $texto .= '<option value='.$value.' selected >' . $value . '</option>';}   
                       else { $texto .= '<option value='.$value.'>' . $value . '</option>';}
                      }
  $texto .=  '</select>';                     
  $texto .=  '</td><td width="200"><label>
               Hora de fin</label></td><td>';
                $optionsXML = simplexml_load_file("../xml/hours.xml"); 
                  $options =  $optionsXML->xpath("//hour/text()");
                  $texto .= '<select id="hour2" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) {  
                   if($hour2==$value){   
                    $texto .= '<option value='.$value.' selected>' . $value . '</option>';
                   }else{$texto .= '<option value='.$value.'>' . $value . '</option>';}
                  }
                $texto .=  '</select>';       
                 $optionsXML = simplexml_load_file("../xml/minutes.xml"); 
                  $options =  $optionsXML->xpath("//minute/text()");
                  $texto .= '<select id="minute2" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) { 
                   if($value==$minute2){   
                    $texto .= '<option value='.$value.' selected>' . $value . '</option>';
                   }else
                       {
                        $texto .= '<option value='.$value.'>' . $value . '</option>';
                       }
                  }
                  $texto .=  '</select>';
  $texto .=  '</td>';
  $texto .=  '</tr>';
  if($_SESSION['users']=="administratives-2" || $_SESSION['users']=="managers"){
   $texto .= '<tr>
               <td width="200"><label>Demora</label></td>
               <td><input type="text" DISABLED id="delay" value="'.(isset($delay)? $delay:0).'"> minutos</td>
           </tr>';  
  }
  if($_SESSION['users']!="technicians" && $_SESSION['users']!="clients"){
   $texto .=  '<tr>              
               <td width="200"><label>Notas para cliente</label></td>
               <td colspan=4><textarea rows="2" cols="91" style="resize:none" id="notes_for_owner">'.$notes_for_owner.'</textarea></td>
           </tr>
           <tr>
               <td width="200">Revisado</td>
               <td><select id="revised">
                 <option value="0" ';
                 if($revised==0){ $texto .= "selected";}
                 $texto .= ' >No</option>';
                 $texto .= ' <option value="1" ';
                 if($revised==1){ $texto .= "selected";}
                 $texto .= ' >Si</option>';
    $texto .= '</select></td>';    
    $texto .= '</tr>';                
  }
  $texto .=  '<tr><td><input type="button" id="send" value="Editar" onclick="javascript:editOperation('.$comtec_id_operation.');" />
              <input type="button" id="send" value="Cancelar" onclick="javascript:loadHome()" /></td></tr>';
  $texto .=  '</table>';
  $texto .=  '<input type="hidden" id="comtec_id_machine" value="'.$comtec_id_machine.'" />
              <input type="hidden" id="token" value="'.$token.'" />
              <div id="result"></div> ';
  $texto .=  '</fieldset>';
  echo $texto;
?>

